/**
 *用于封装常用的组件
 *
 */
var $table;
var index;
var flag = true;
(function($){
    // 继承jquery
   $.extend({
       table:{
           // 初始化一个bootstraptable的
           init:function(options){
               // 所有的表格id默认为table
               $table= $("#table").bootstrapTable({
                   columns:options.columns,
                   url:options.url,
                   dataType:'json',
                   method:'post',
                   sidePagination:'server',
                   pagination:true,
                   pageNumber:1,
                   pageSize:10,
                   pageList:[10,20,30],
                   contentType:'application/x-www-form-urlencoded',
                   showRefresh:true,// 显示刷新
                   showToggle:true, // 开启卡片视图
                   showFullscreen:true,// 开启全频
                   showColumns:true,
                   showPaginationSwitch:true,
                   total:100,
                   toolbar:"#tools",
                   theadClasses:'thead-dark',
                   headerStyle:function(column){
                       return {css:{"background-color":"blue"}};
                   },
                   responseHandler:function(res,jqXHR){
                       // 在url发送异步请求后执行，作用处理异步请求的响应结果
                       var result = {"rows":res.data.list,"total":res.data.total};
                       return result;
                   },
                   queryParams:function(params){
                       params.pageNumber=this.pageNumber;
                       params.pageSize=params.limit;
                       // 插叙列表页中只能有一个form表单
                       let fields = $("form").serializeArray();
                       jQuery.each( fields, function(i, field){
                           params[field.name]=field.value
                       });
                       return params;
                   }
               });
           },
           refresh:function(){
               $table.bootstrapTable('refresh');
           },
           reset:function(){
               $("form")[0].reset();
               $.table.refresh();
           }

       },
       tableTree:{
           init:function(options){
               $table = $("#table");
               $table.bootstrapTable({
                   url: options.url,// 请求返回的是json数组
                   contentType:'application/x-www-form-urlencoded',
                   idField: options.idField, //指明哪个字段将用作复选框/单选框值，
                   showColumns: true,// 是否在工具栏上显示列选项
                   showRefresh:true,// 显示刷新
                   showToggle:true, // 开启卡片视图
                   showFullscreen:true,// 开启全频
                   toolbar:'#tools',
                   columns: options.columns,// 声明列以及为列绑定数据
                   treeShowField: options.treeShowField, // 声明树在哪一列上显示
                   parentIdField: options.parentIdField, // 声明一个作为父级id的列的列名
                   method:'post',
                   responseHandler:function(res,jqXHR){
                       // 处理res  json对象
                       return res.data;
                   },
                   queryParams:function(params){
                       // 插叙列表页中只能有一个form表单
                       let fields = $("form").serializeArray();
                       jQuery.each( fields, function(i, field){
                           params[field.name]=field.value
                       });
                       return params;
                   },
                   onPostBody: function() {
                       // 在表体呈现并在DOM中可用之后触发
                       var columns = $table.bootstrapTable('getOptions').columns

                       if (columns && columns[0][1].visible) {
                           // 在表格中渲染树
                           $table.treegrid({
                               treeColumn: 1,
                               onChange: function() {
                                   $table.bootstrapTable('resetView')
                               }
                           })
                       }
                   }
               })
           },
           expandOrCollspse:function(){
               if(flag){
                   $table.treegrid("collapseAll");
                   flag = false;
               }else{
                   $table.treegrid("expandAll");
                   flag = true;
               }
           }
       },
       operator:{
           get:function(url,data,callback){
               $.ajax({
                   type:'get',
                   dataType:'json',
                   url:url,
                   data:data,
                   success:function(ret){
                       if(typeof(callback)=='function'){
                           callback(ret);
                       }
                       $.modal.successMsg(ret.msg)
                       $table.bootstrapTable('refresh');
                   },
                   error:function(ret){
                       $.modal.errorMsg();
                   }
               })
           },
           post:function(url,data,callback){
               $.ajax({
                   type:'post',
                   dataType:'json',
                   url:url,
                   data:data,
                   success:function(ret){
                       if(typeof(callback)=='function'){
                           callback(ret);
                       }else{
                       $.modal.successMsg(ret.msg)
                       $.modal.closeWin(parent.index);
                       parent.$table.bootstrapTable('refresh');
                       }
                   },
                   error:function(ret){
                       $.modal.errorMsg();
                   }
               })
           },
           delete:function(url,data,callback){
               $.ajax({
                   type:'delete',
                   dataType:'json',
                   url:url,
                   data:data,
                   success:function(ret){
                       if(typeof(callback)=='function'){
                           callback(ret);
                       }
                       $.modal.successMsg(ret.msg)
                       $table.bootstrapTable('refresh');
                   },
                   error:function(ret){
                      $.modal.errorMsg();
                   }
               })
           }
       },
       modal:{
           add:function(title,url,area){
               $.modal.addWin(title,url,area);
           },
           edit:function(title,url,area){
               $.modal.editWin(title,url,area);
           },
           editTools:function(idProperty,title,url){
               let selectData = $table.bootstrapTable('getSelections');
               if(selectData.length!=1){
                   layer.msg('请选中一条需要修改的数据',{offset: '100px',icon:-1,shade:0,area:["400px","50px"],anim: 1,time:5000});
                   return;
               }
               let userId = selectData[0];
               userId = userId[idProperty];
               $.modal.edit(title,url+userId);
           },
           remove:function(url,id){
               layer.confirm('是否确定删除id为：'+id+'的数据',{icon: 3, title:'提示'},function(index){
                   layer.close(index);
                   $.operator.delete(url);
               })
           },
           removes:function(idProperty,url){
               let selectData = $table.bootstrapTable('getSelections');
               if(selectData.length==0){
                   $.modal.successMsg('请选中需要删除的数据');
                   return;
               }
               let ids=[];
               for(var i=0;i<selectData.length;i++){
                   var obj = selectData[i];
                   ids.push(obj[idProperty]);
               }
               layer.confirm('确认要删除id为：'+ids+'的数据吗？',function(index){
                   layer.close(index);
                   $.operator.delete(url,{ids:ids});
               })
           },
           closeWin:function(index){
             layer.close(index);
           },
           addWin:function(title,url,area){
               if(area==undefined){
                   area= ['900px','300px'];
               }

               index =  layer.open({
                   type:2,
                   title:title,
                   area:area,
                   content: url,
                   closeBtn:2,
                   resize: true,
                   fix: false,
                   maxmin:true

               })
           },
           addWinBtns:function(title,url,area,sure){
               if(area==undefined){
                   area= ['900px','300px'];
               }
               index =  layer.open({
                   type:2,
                   title:title,
                   area:area,
                   content: url,
                   closeBtn:2,
                   resize: true,
                   maxmin: true,
                   btn:['确认','关闭'],
                   yes:function(index,layero){
                       if(typeof(sure)=='function'){
                           sure(index,layero);
                       }
                   },
                   btn2:function(index,layero){
                       layer.close(index);
                   }

               })
           },
           editWin:function(title,url,area){
               if(area==undefined){
                   area= ['900px','300px'];
               }
               index =  layer.open({
                   type:2,
                   title:title,
                   area:area,
                   content: url,
                   closeBtn:2,
                   maxmin: true,
                   resize: true
               })
           },
           successMsg:function(message){
               layer.msg(message,{offset: '100px',icon:-1,shade:0,area:["400px","50px"],anim: 1,time:3000});
           },
           errorMsg:function(){
               layer.msg('网络错误',{offset: '100px',icon:-1,shade:0,area:["400px","50px"],anim: 1,time:5000});
           }
       }
   })
})(jQuery);